package br.pucrio.inf.aaaong.models.form.converters.atomic;

import java.util.List;

import br.pucrio.inf.aaaong.models.form.errors.FormError;


public class FormFaultTolerantConverter<T extends FormConverter<K>, K> {
	private FormConverter converter;
	private List<FormError> errors;

	public FormFaultTolerantConverter(FormConverter<K> converter, List<FormError> errors) {
		this.converter = converter;
		this.errors = errors;
	}
	
	public K convert(String str, String fieldName) {
		if (converter.canConvert(str)) {
			K converted = (K) converter.convert(str);
			return converted;
		} else {
			FormError error = new FormError(converter.getConversionErrorMessage(), fieldName);
			errors.add(error);
			return null;
		}
	}
	
}
